﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Ellipse : MonoBehaviour
{
    //保存需要排序的精灵容器
    public List<GameObject> Sprites = new List<GameObject>();
    public Transform centerPoint;//椭圆的中心点
    public float anglecheap = 45;//每个方块间的角度偏移
    //保存位置点
    private List<Vector3> location = new List<Vector3>();
    private float angle = 270;//第一个保证是中心位置的,当前到的角度
    private float firstangle = 270;//记录第一个角度，用以左右对称
    [Header("Y轴小半径")]
    public float r = 90;//椭圆的两个弦长
    [Header("X轴小半径")]
    public float R = 200;
    int size = 0;//即对象数组的一半，奇数为总长度+1/2,偶数为一半<pre name="code" class="csharp">// Use this for initialization
    private int deep = 0;
    public GameObject prb_item;
    public GameObject root;
    void Start()
    {
        CreateItemList();
        //初始化size
        if (Sprites.Count % 2 == 0)
        {
            size = Sprites.Count / 2;
        }
        else
        {
            size = (Sprites.Count + 1) / 2;
        }
        //排序分级显示
        makespriteSort();
        //重置渲染层级
        ResetDeep();
    }
    void CreateItemList()
    {
        for (int i = 0; i < 7; i++)
        {
            GameObject go = Instantiate(prb_item) as GameObject;
            go.transform.parent = root.transform;
            go.transform.localScale = Vector3.one;
            go.transform.localPosition = Vector3.zero;
            go.GetComponent<MenueItem>().SetData(i);
            Sprites.Add(go);
        }
    }

    //给这些精灵排序显示
    void makespriteSort()
    {
        //取出椭圆的中心点
        Vector3 center = centerPoint.position;
        //判断该数组的个数奇偶性，如果是偶数，那么需要留出一个来放到对面
        if (Sprites.Count % 2 == 0)
        {
            //右半边
            for (int i = 0; i < size; i++)
            {
                Sprites[i].transform.position = getPosition(angle, center);
                //       m_rightsprite.Add(Sprites[i]);
                angle += anglecheap;
            }
            //第一个已经得是左边了
            angle = firstangle - anglecheap;
            //左半边
            for (int i = size; i < Sprites.Count - 1; i++)
            {
                Sprites[i].transform.position = getPosition(angle, center);
                angle -= anglecheap;
                //  m_leftsprite.Add(Sprites[i]);
            }
            //最后一个
            Sprites[Sprites.Count - 1].transform.position = getPosition(firstangle - 180, center);
            // m_leftsprite.Add(Sprites[Sprites.Count - 1]);
            return;
        }
        //如果不是偶数，那么出去中间那个，正好正常显示
        else
        {
            //右半边
            for (int i = 0; i < size; i++)
            {
                Sprites[i].transform.position = getPosition(angle, center);
                //    m_rightsprite.Add(Sprites[i]);
                angle += anglecheap;
            }
            //第一个已经得是左边了
            angle = firstangle - anglecheap;
            //左半边
            for (int i = size; i < Sprites.Count; i++)
            {
                Sprites[i].transform.position = getPosition(angle, center);
                //  m_leftsprite.Add(Sprites[i]);
                angle -= anglecheap;
            }
            return;
        }

    }
    //获取当前角度的坐标
    Vector3 getPosition(float _angle, Vector3 _centerposition)
    {
        float hudu = (_angle / 180f) * Mathf.PI;
        float cosx = Mathf.Cos(hudu);
        float sinx = Mathf.Sin(hudu);
        float x = _centerposition.x + R * cosx;
        float y = _centerposition.y + r * sinx;
        Vector3 point = new Vector3(x, y, 0);
        //添加到容器保存
        location.Add(point);
        return point;
    }

    //根据当前左右容器调整所有控件的渲染层级
    void ResetDeep()
    {
        deep = 0;
        //右半边
        for (int i = size - 1; i >= 0; i--)
        {
            Sprites[i].GetComponent<Transform>().SetSiblingIndex(deep);
            deep++;
        }
        deep = 0;
        //左半边
        for (int i = Sprites.Count - 1; i >= size; i--)
        {
            Sprites[i].GetComponent<Transform>().SetSiblingIndex(deep);
            deep++;
        }
    }
}
